package com.wythe.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wythe.mall.R;
import com.wythe.mall.model.ClassifyBean;
import com.wythe.mall.utils.DPIUtil;
import com.wythe.mall.utils.DensityUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //1.7 泛型自动推断

    private List<ClassifyBean> mList;
    private Context mContext;
    private int screenWidth;
    private OnItemClickListener onItemClickListener;

    public MyGridAdapter(Context context, List<ClassifyBean> list) {
        mContext = context;
        mList = list;
        screenWidth = DensityUtils.screenWidth(context);
    }

    //条目数量

    @Override

    public int getItemCount() {

        return mList.size();

    }

    //RecyclerView.ViewHolder是缓存条目控件

    @Override

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        View itemView=View.inflate(parent.getContext(),itemId,null);

        //1.条目布局

        //2.RecyclerView  传入给item参考宽度

        //3.是否把打气布局添加到父控件 true添加,报错 二次添加  false,不添加 仅参考宽度

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid_label, parent, false);

        MyViewHolder myViewHolder = new MyViewHolder(itemView);

        return myViewHolder;

    }

    @Override

    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        MyViewHolder hd = (MyViewHolder) holder;
        View rootView = hd.itemView.getRootView();
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) rootView.getLayoutParams();
        params.width = (int) ((screenWidth - DPIUtil.dip2px(mContext, 96)) / 5);
        rootView.setLayoutParams(params);
        //数据

        final ClassifyBean item = mList.get(position);

        hd.text.setText(item.getTitle());
        Glide.with(mContext).load(item.getImage())
                .dontAnimate()
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(hd.img);

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(position, item);
                }
            }
        });

        //字段

    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public interface OnItemClickListener {

        void onClick(int position, ClassifyBean bean);

    }


    static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView text;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.home_category);
            text = itemView.findViewById(R.id.tv_name);
        }
    }
}

